package cn.dnuihyy406.dnuifood_16110100406.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.dnuihyy406.dnuifood_16110100406.MainActivity;
import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.UserBean;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.UserModel;

public class LoginActivity extends BaseActivity
        implements OnRetrofitListener<UserBean>,View.OnClickListener{
    private TextView Username,Password;
    private Button Login,Register;
    private EditText E_Username,E_Password;
    private TextView forgetPwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // layout_file=R.layout.activity_login;
        forgetPwd=findViewById(R.id.forgetPwd);
        forgetPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(LoginActivity.this,ForgetPwdActivity.class);
                startActivity(intent2);
                Toast.makeText(LoginActivity.this,"forgetPwd",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void initViews() {
        layout_file=R.layout.activity_login;
        setLayout(layout_file);
        E_Username=findViewById(R.id.user_name);
        E_Password=findViewById(R.id.pass_word);
        Login=findViewById(R.id.login);
        Register=findViewById(R.id.register);


    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login:
                if (E_Username.getText().toString().trim().equals("")||E_Password.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名和密码不能空",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    login();
                }
                break;
            case R.id.register:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                Toast.makeText(LoginActivity.this,"Register",Toast.LENGTH_SHORT).show();
                break;
//            case R.id.forgetPwd:
//                Intent intent2 = new Intent(LoginActivity.this,ForgetPwdActivity.class);
//                startActivity(intent2);
//                Toast.makeText(LoginActivity.this,"forgetPwd",Toast.LENGTH_SHORT).show();
//                break;
            default:
                break;
        }
          //监听控件
    }

    void login(){
        UserModel model =new UserModel();
        model.login(E_Username.getText().toString().trim(),E_Password.getText().toString().trim(),this);
//   利用retrofit进行登录验证
    }
    @Override
    public void initEvents() {
        Login.setOnClickListener(this);
        Register.setOnClickListener(this);
    }


    @Override
    public void initData() {

    }

    @Override
    public void onSuccess(UserBean object,int flag) {
        if(object.getUserid().equals("0")){
            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }
        else{

            Intent intent =new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            saveUser(object.getUserid());
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
        }
    }
    //在loginactivity中调用usermodel的请求发送，处理retrofit请求成功的结果
    @Override
    public void onFailure(String msg) {
        Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();
    }
//在loginactivity中调用usermodel的请求发送，处理retrofit请求失败的结果

    public void saveUser(String user_id){
        SharedPreferences sharedPreferences =getSharedPreferences("hyy", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
//        editor.putString("username",E_Username.getText().toString());
        editor.putString("userid",user_id);
//        int userid =Integer.parseInt(user_id);
//        editor.putInt("user_id",userid);
//        editor.putString("userpass",E_Password.getText().toString());
        editor.commit();
    }
//对用户的存储方法，因为调用了用户的信息存储方法，保存登录用户的信息


}